package com.rms.hkpc.fragment.base;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.rms.hkpc.R;
import com.rms.hkpc.constant.AllConstants;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseChildrenFragment extends Fragment {
    protected Context context;
    private View view;
    protected ImageView ivFilter;
    protected Fragment mFragment;
    protected List<Fragment> fragmentList;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(getLayoutId(), container, false);
        context = getActivity();
        EventBus.getDefault().register(this);
        fragmentList = new ArrayList<>();
        TextView tvNav = fd(R.id.tv_nav);
        tvNav.setText(context.getResources().getString(navTitle()));
        ivFilter = fd(R.id.iv_filter);
        ivFilter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ivFilter.setVisibility(View.GONE);
                filterClick();
            }
        });
        initFragment();
        initView();
        return view;
    }

    protected abstract int getLayoutId();

    protected abstract void initFragment();

    protected abstract void initView();

    protected abstract void filterClick();

    protected abstract int navTitle();

    protected <I extends View> I fd(int id) {
        return view.findViewById(id);
    }

    protected void showFragment(Fragment fromFragment, Fragment toFragment) {
        if (fromFragment != toFragment) {
            mFragment = toFragment;
            FragmentManager fm = getChildFragmentManager();
            FragmentTransaction transaction = fm.beginTransaction();
            if (fromFragment != null) {
                transaction.hide(fromFragment);
            }
            if (toFragment.isAdded()) {
                transaction.show(toFragment);
            } else {
                transaction.add(R.id.fl_content, toFragment);
            }
            transaction.commit();
        }
    }
}
